//
//  Setter.m
//  Staff
//
//  Created by caiyao on 1/13/15.
//
//

#import "Setter.h"
#import "NSData-AES.h"

@implementation Setter

- (void)excute {
    NSString *key = [self.command.functionParamsDic objectForKey:@"key"];
    
    if (key == nil || self.command.dataJS == nil)
    {
        // params is missing fail to handle
        [self excuteNonHookFoundCallback];
        return;
    }
    
    NSString *jsCall = [NSString stringWithFormat:@"%@();",self.command.dataJS];
    NSString *dataResponse = [self.webView stringByEvaluatingJavaScriptFromString:jsCall];
    
    if (dataResponse == nil)
    {
        // params is missing fail to handle
        [self excuteNonHookFoundCallback];
        return;
    }
    
    DebugLog(@"dataResponse:%@",dataResponse);
    
    // use the data base record
    NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
    
    if([key isEqualToString:@"customerInfo"])
    {
        NSData *data=[[dataResponse dataUsingEncoding:NSUTF8StringEncoding]
                      AES256EncryptWithKey:secureToken];
        [defs setObject:data forKey:key];
    }
    else
    {
        [defs setObject:dataResponse forKey:key];
    }
    
    [defs synchronize];
    
    [self excuteCallBackJSWithParameters:nil];
}

@end
